package com.wedo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.activity.LoginActivity;
import com.wedo.mall.activity.goods.GoodsDetailActivity;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.activity.user.MyCollectionActivity;
import com.wedo.mall.activity.user.PointGoodsDetailActivity;
import com.wedo.mall.dialog.SimpleDialog;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.fragment.CollectionGoodsFragment;
import com.wedo.mall.util.WUrl;
import com.wedo.mall.util.WUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * Created by lxy on 2017/6/19.
 */

public class GoodsAdapter extends Adapter<Goods> {

    /**是否是积分商城中的商品，如果是积分商城的商品，则显示单位为point*/
    public boolean intergration = false;
    public Boolean isCollectionPage = false;
    public OnCollectionGoodsFragmentListener ocgflistenr;

    //private String goods_sale;
    private String goods_point;

    public GoodsAdapter(Context context, List<Goods> datas) {
        super(context, datas);
        Resources res = context.getResources();
        //goods_sale = res.getString(R.string.goods_sale);
        goods_point = res.getString(R.string.goods_price);

    }

    @Override
    protected int layout() {
        return R.layout.item_goods;
    }

    @Override
    protected void bind(EHolder holder, final Goods goods, int position) {
        ImageView iv_img = holder.find(R.id.iv_img);
        ImageView iv_auth = holder.find(R.id.iv_auth);
        TextView tv_name= holder.find(R.id.tv_name);
        TextView tv_price = holder.find(R.id.tv_price);
        TextView tv_sale = holder.find(R.id.tv_sale);
        ImageView iv_shopping = holder.find(R.id.iv_shopping);
        setShoppingListener(iv_shopping,goods);

        RelativeLayout rl_root = holder.find(R.id.rl_root);

        iv_auth.setVisibility(goods.auth()?View.VISIBLE:View.INVISIBLE);
        tv_name.setText(goods.goods_name);
        //tv_sale.setText(String.format(goods_sale,goods.sale_number));

        if (!intergration){
            tv_price.setText(String.format(goods_point,goods.fixPrice()));
        }else {
            /**积分商城*/
            tv_price.setText(String.format("%s point(s)",goods.price()));
        }

        String img = WUrl.url(goods.goods_image);
        Glide.with(mContext).load(img).error(R.drawable.place_holder_goods).placeholder(R.drawable.place_holder_goods).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_img);

        if(isCollectionPage){
            rl_root.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    SimpleDialog dialog = SimpleDialog.with(mContext);
                    dialog.setQuestion(mContext.getString(R.string.deleteCollection));
                    dialog.listener = new SimpleDialog.OnEnsureListener() {
                        @Override
                        public void onEnsure() {
                            delCollect(goods.collect_id);
                        }
                    };
                    dialog.show();
                    return true;
                }
            });
        }
    }

    public void listener(final EActivity activity){
        onItemClickListener = new EListener<Goods>() {
            @Override
            public void onInvoked(View view, Goods data, int position) {
                GoodsDetailActivity.toDetail(activity,data.id);
            }
        };
    }

    public void collectionGoodsListener(final EActivity activity){
        onItemClickListener = new EListener<Goods>() {
            @Override
            public void onInvoked(View view, Goods data, int position) {
                GoodsDetailActivity.toDetail(activity,data.goods_id);
            }
        };
    }

    //判断是否为收藏页面
    public void isCollection(boolean b){
        isCollectionPage = b;
    }

    public void pointGoodslistener(final EActivity activity){
        onItemClickListener = new EListener<Goods>() {
            @Override
            public void onInvoked(View view, Goods data, int position) {
                PointGoodsDetailActivity.toDetail(activity,data.id);
            }
        };
    }

    private void setShoppingListener(ImageView iv_shopping, final Goods goods){
        iv_shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopping(goods);
            }
        });
    }

    /**加入购物车*/
    private void shopping(Goods goods){

        if(!goods.payable()) {
            WUtils.showTipsDialog(mContext);
            return;
        }

        if(!WLogin.logged(mContext)) {
            Intent intent = new Intent(mContext, LoginActivity.class);
            intent.putExtra(LoginActivity.EXTRA_ENDPOINT,LoginActivity.ENDPOINT_MAIN);
            mContext.startActivity(intent);
            return;
        }

        if (!WLogin.isBinding(mContext)){
            return;
        }

        Observable<EResponse> observable = EClient.with().api().addShoppingCar(goods.id,"1","0");
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                if (mOnShoppingListener !=null){
                    mOnShoppingListener.onAdd2Car(1);
                }
                Toast.makeText(mContext,R.string.goods_add_to_car_successful,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private OnShoppingListener mOnShoppingListener;

    public void setOnShoppingListener(OnShoppingListener onShoppingListener) {
        this.mOnShoppingListener = onShoppingListener;
    }

    public interface OnShoppingListener{
        void onAdd2Car(int number);
    }

    /**
     * 删除收藏
     * @param
     */
    private void delCollect(String id){

        Map<String,String> params = new HashMap<>();
        params.put("ids",id);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_DELCOLLECT,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                ocgflistenr.onRefreshClick();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * fragment回调
     */
    public interface OnCollectionGoodsFragmentListener {
        void onRefreshClick();
    }

    /**
     * 监听
     * @param Listener
     */
    public void setOnRefreshClickListener (OnCollectionGoodsFragmentListener  Listener) {
        this.ocgflistenr = Listener;
    }
}
